package developer.算法.二叉树.二叉树最大深度;

/**
 * @author zhangyongkang
 * @time 2024/6/25 15:39
 * @description 104. 二叉树的最大深度
 * 简单
 * 相关标签
 * 相关企业
 * 给定一个二叉树 root ，返回其最大深度。
 * <p>
 * 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
 */
public class Solution_self {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */

    public static void main(String[] args) {

        TreeNode treeNode = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(3));
        Solution4 solution3 = new Solution4();
        System.out.println(solution3.maxSize(treeNode));

    }


    static class Solution4 {
        public int maxSize(TreeNode root) {
            return dfs(root);
        }

        private int dfs(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftVal = dfs(root.left);
            int rightVal = dfs(root.right);
            return Math.max(leftVal, rightVal) + 1;
        }
    }


    static class Solution3 {
        public int maxSize(TreeNode root) {
            return handle(root);
        }

        private int handle(TreeNode node) {
            if (node == null) return 0;
            return Math.max(handle(node.left), handle(node.right)) + 1;
        }
    }

    static class Solution {
//        public int maxDepth(TreeNode root) {
//            if(Objects.isNull(root)){
//                return 0;
//            }else{
//                int left = maxDepth(root.left);
//                int right = maxDepth(root.right);
//                return Math.max(left,right)+1;
//            }
//
//        }

        /**
         * 1
         * 2    2
         * 3
         * 4
         *
         * @param root
         * @return
         */
        public static void main(String[] args) {
            TreeNode treeNode = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(3));
            int i = maxSize(treeNode);
            System.out.println(i);
        }

        public static int maxSize(TreeNode root) {
            if (root == null) return 0;
            int left = maxSize(root.left);
            int right = maxSize(root.right);
            return Math.max(left, right) + 1;
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
